package com.example.emas_android_demo;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class TodoAdapter extends RecyclerView.Adapter<TodoAdapter.TodoViewHolder> {
    private List<Todo> todoList;

    private Context context;

    public TodoAdapter(Context context, List<Todo> todoList) {
        this.context = context;
        this.todoList = todoList;
    }

    @NonNull
    @Override
    public TodoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.todo_item, parent, false);
        return new TodoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TodoViewHolder holder, int position) {
        Todo todo = todoList.get(position);
        holder.titleTextView.setText(todo.getTitle());
        holder.iconImageView.setOnClickListener(v -> {
            Intent intent = new Intent(context, TodoDetailActivity.class);
            intent.putExtra("TITLE", todo.getTitle());
            intent.putExtra("DESC", todo.getDesc());
            intent.putExtra("STATUS", todo.getStatus());
            intent.putExtra("PRIORITY", todo.getPriority());
            intent.putExtra("EXPECTTIME", todo.getExpectTime());
            context.startActivity(intent);
        });
        //holder.completedTextView.setText(todo.isCompleted() ? "Completed" : "Not Completed");
    }

    @Override
    public int getItemCount() {
        return todoList.size();
    }

    static class TodoViewHolder extends RecyclerView.ViewHolder {
        TextView titleTextView;
        ImageView iconImageView;

        public TodoViewHolder(View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.titleTextView);
            iconImageView = itemView.findViewById(R.id.iconImageView);
        }
    }
}
